//
//  ReceiptAdressTableViewCell.m
//  ChunLaiZhuangClient
//
//  Created by xielihui on 16/3/12.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "ReceiptAdressTableViewCell.h"
#import "AddressListModel.h"
@interface ReceiptAdressTableViewCell()
@property(nonatomic, strong) AddressSubModel *model;
@end

@implementation ReceiptAdressTableViewCell
- (void)awakeFromNib {
    self.contentView.left = 0.f;
    // Initialization code
}

- (void)updateAddressInfo:(AddressSubModel *)model {
    self.model = model;
    self.addressLabel.text = model.formatted_address;
    self.phoneLabel.text = model.telephone;
    self.nameLabel.text = model.firstname;
    [self.model addObserver:self forKeyPath:@"isChecked" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];

}

- (IBAction)chooseAddressAction:(id)sender {
    self.model.isChecked = !self.model.isChecked;
    if(self.selectAddressBlock) {
        if (self.model.isChecked) {
            self.selectAddressBlock(self.model);
        } else {
            self.selectAddressBlock(nil);
        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setBtnImages {
    if (self.model.isChecked) {
        [self.chooseBtn setImage:[UIImage imageNamed:@"btn_select_selectd"] forState:UIControlStateNormal];
    } else {
        [self.chooseBtn setImage:[UIImage imageNamed:@"btn_select_nor"] forState:UIControlStateNormal];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"isChecked"]) {
        [self setBtnImages];
    }
}

- (void)dealloc {
    [self.model removeObserver:self forKeyPath:@"isChecked"];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
